<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>weighted_avg 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-weight-avg-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-weight-avg-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-weight-avg-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-weight-avg-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-weight-avg-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">weighted_avg 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-avg-aggregation.html">« avg 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-boxplot-aggregation.html">boxplot 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-weight-avg-aggregation"></a>加权平均(weighted_avg)聚合
</h2>
</div></div></div>
<p>
<code class="literal">single-value(单值)</code>度量聚合，计算从聚合文档中提取的数值的加权平均值。

这些值可以从文档中指定的 numeric 字段中提取。
</p>
<p>
计算常规平均值时，每个数据点都有相同的“权重(weight)”…它对最终的值的贡献是相同的。

加权平均值就不同了，它会对每个数据点进行不同的加权。

每个数据点对最终的值的贡献量是从文档中提取的，或者由脚本提供。
</p>
<p>
加权平均值公式是 <code class="literal">∑(value * weight) / ∑(weight)</code>
</p>
<p>
常规平均值也可视为加权平均值，只是每个值的隐含权重为<code class="literal">1</code>。
</p>
<div class="table">
<a id="weighted-avg-params"></a>
<p class="title"><strong>表 3. <code class="literal">weighted_avg</code> 参数</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="weighted_avg Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">参数名称</th>
<th align="left" valign="top">描述</th>
<th align="left" valign="top">要求</th>
<th align="left" valign="top">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">value</code></p></td>
<td align="left" valign="top"><p>提供值的字段或脚本的配置</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p>&emsp;&emsp;&emsp;</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">weight</code></p></td>
<td align="left" valign="top"><p>提供权重的字段或脚本的配置</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">format</code></p></td>
<td align="left" valign="top"><p>数值响应格式化程序</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">value_type</code></p></td>
<td align="left" valign="top"><p>关于纯脚本或未映射字段的值的提示</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>
<code class="literal">value</code> 和 <code class="literal">weight</code> 对象每个字段可有特定的配置：
</p>
<div class="table">
<a id="value-params"></a>
<p class="title"><strong>表 4. <code class="literal">value</code> 参数</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="value Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">参数名称</th>
<th align="left" valign="top">描述</th>
<th align="left" valign="top">要求</th>
<th align="left" valign="top">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">field</code></p></td>
<td align="left" valign="top"><p>要从其中提取值的字段</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">missing</code></p></td>
<td align="left" valign="top"><p>字段完全缺失时使用的值</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="table">
<a id="weight-params"></a>
<p class="title"><strong>表 5. <code class="literal">weight</code> 参数</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="weight Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">参数名称</th>
<th align="left" valign="top">描述</th>
<th align="left" valign="top">要求</th>
<th align="left" valign="top">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">field</code></p></td>
<td align="left" valign="top"><p>要从其中提取权重的字段</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">missing</code></p></td>
<td align="left" valign="top"><p>字段完全缺失时使用的权重值</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_examples_3"></a>例子
</h3>
</div></div></div>
<p>
如果我们的文档有一个保存 0-100 数值分数的 <code class="word">"grade"</code> 字段和一个保存任意数值权重的 <code class="word">"weight"</code> 字段，我们可以使用以下公式计算加权平均值：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search
{
    "size": 0,
    "aggs" : {
        "weighted_grade": {
            "weighted_avg": {
                "value": {
                    "field": "grade"
                },
                "weight": {
                    "field": "weight"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/325.console"></div>
<p>这将产生如下响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "weighted_grade": {
            "value": 70.0
        }
    }
}</pre>
</div>
<p>
虽然每个字段允许多个值<code class="remark">((multiple values-per-field)</code>，但只允许一个权重。

如果聚合遇到具有多个权重的文档(例如，权重字段是多值字段<code class="remark">(multi-value field)</code>)，将抛出异常。

如果遇到这种情况，需要为权重字段指定一个 <code class="literal">script</code> ，并使用该脚本将多个值组合成一个要使用的值。
</p>
<p>
这个权重将独立应用于从 <code class="literal">value</code> 字段中提取的每个值。
</p>
<p>
下面这个示例显示了如何使用单一权重对具有多个值的单个文档进行平均：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_doc?refresh
{
    "grade": [1, 2, 3],
    "weight": 2
}

POST /exams/_search
{
    "size": 0,
    "aggs" : {
        "weighted_grade": {
            "weighted_avg": {
                "value": {
                    "field": "grade"
                },
                "weight": {
                    "field": "weight"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/326.console"></div>
<p>
<code class="word">grade</code>的三个值(<code class="literal">1</code>、<code class="literal">2</code> 和 <code class="literal">3</code>)将作为独立的值包含在内，所有值的权重为<code class="literal">2</code>:
</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "weighted_grade": {
            "value": 2.0
        }
    }
}</pre>
</div>
<p>
聚合返回的结果是 <code class="literal">2.0</code>，这与我们手动计算时的预期相符:
<code class="literal">((1*2) + (2*2) + (3*2)) / (2+2+2) == 2</code>
</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script_2"></a>脚本(script)
</h3>
</div></div></div>
<p>
<code class="literal">value</code>和<code class="literal">weight</code>都可以从脚本中导出，而不是从字段中。

举个简单的例子，下面将使用脚本在文档中给<code class="word">grade</code>和<code class="word">weight</code>加 1：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search
{
    "size": 0,
    "aggs" : {
        "weighted_grade": {
            "weighted_avg": {
                "value": {
                    "script": "doc.grade.value + 1"
                },
                "weight": {
                    "script": "doc.weight.value + 1"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/327.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_values"></a>缺失的值(missing values)
</h3>
</div></div></div>
<p>
参数 <code class="literal">missing</code> 定义应该如何处理有缺失值的文档。

<code class="literal">value</code> 和 <code class="literal">weight</code>的默认行为不同：
</p>
<p>
默认情况下，如果 <code class="literal">value</code> 字段缺失，则忽略该文档，并继续聚合下一个文档。

如果 <code class="literal">weight</code> 字段缺失，则假设权重为 <code class="literal">1</code> (类似于常规平均值)。
</p>
<p>
这两个默认值都可以用参数 <code class="literal">missing</code> 覆盖：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search
{
    "size": 0,
    "aggs" : {
        "weighted_grade": {
            "weighted_avg": {
                "value": {
                    "field": "grade",
                    "missing": 2
                },
                "weight": {
                    "field": "weight",
                    "missing": 3
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/328.console"></div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-avg-aggregation.html">« avg 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-boxplot-aggregation.html">boxplot 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>